package com.anon_soft.anon_edit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region.Op;
import android.util.AttributeSet;
import android.widget.EditText;
import android.util.Log;

public class ASEditText extends EditText {
	
	/* Members */
	
	private		boolean		m_textHasChanged;
	private		boolean		m_drawLineNumbers;
	private		boolean		m_drawGutterLine;
	private		boolean		m_highlightText;
	private		int			m_gutterSize;
	private		Rect		m_gutterLineRect;
	private		Paint		m_gutterLinePaint;
	private		Paint		m_gutterTextPaint;
	
	private		String		TAG = "ASEditText";

	public ASEditText(Context context) {
		super(context);
		setDefaults();
	}
	
	public ASEditText(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		setDefaults();
	}
	
	public ASEditText(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		setDefaults();
	}
	
	public 	void	setDefaults()
	{
		m_textHasChanged = false;
		m_drawLineNumbers = true;
		m_drawGutterLine = true;
		m_highlightText = true;
		m_gutterSize = 60;
		
		m_gutterLineRect = new Rect();
		m_gutterLinePaint = new Paint();	
		m_gutterTextPaint = new Paint();
		m_gutterTextPaint.setTextAlign(Paint.Align.RIGHT);
		m_gutterTextPaint.setARGB(255, 255, 0, 0);
		m_gutterLinePaint.setARGB(255, 255, 255, 255);
			
		/*
		m_gutterLinePaint = new Paint();
		m_gutterLinePaint.setARGB(0, 256, 0, 0);
		*/
		
		/*
		 * Set padding for left side to make room for gutter
		 */
		
		super.setPadding(m_gutterSize, 5, 5, 5);
		super.setHorizontallyScrolling(true);
		
	}
	
	public	void	acknowledgeChangedText()
	{
		m_textHasChanged = false;
	}
	
	public	boolean	needToBeSaved()
	{
		return m_textHasChanged;
	}
	
	public	void	drawLineNumbers(boolean d)
	{
		m_drawLineNumbers = d;
	}
	
	public	void	drawGutterLine(boolean g)
	{
		m_drawGutterLine = g;
	}
	
	public	void	setGutterSize(int g)
	{
		m_gutterSize = g;
	}
	
	public	void	setGutterLineColor(int a, int r, int g, int b)
	{
		m_gutterLinePaint.setARGB(a, r, g, b);
	}
	
	public	void	setGutterTextColor(int a, int r, int g, int b)
	{
		m_gutterTextPaint.setARGB(a, r, g, b);
	}
	
	public	boolean	isDrawingLineNumbers()
	{
		return m_drawLineNumbers;
	}

	public	boolean	isDrawingGutterLine()
	{
		return m_drawGutterLine;
	}
	
	public	int		getGutterSize()
	{
		return m_gutterSize;
	}

	
	/**
	 * Draw View contents, including some custom items like the gutter line,
	 * line numbers and text highlighting.
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		
		/* TODO: ASEditText onDraw tasks
		 * 
		 * + Set the padding so that we have space to draw the gutter
		 * + Draw the line separator 
		 * + Draw line numbers if desired
		 * - Clean up code
		 * - Add comments
		 * - Add options for object behavior and getters and setters
		 * - Fix bug on Xoom where the tab fragments get their text jumbled up
		 * - Implement proper word wrap / line number drawing.  Right now EditText
		 *   thinks that wrapping an extended line to the next line counts as an 
		 *   additional line!!!  We might need to make our line counting code smarter
		 *   (ie. look at actual text content when drawing line numbers)
		 */
		
		int		lineCount = 0;
		Rect	drawingRect = new Rect();
		Rect	originalClipRect = new Rect();
	
		
		// Draw the super class first
		// (Doing this fixed a bug where the gutter line
		//  wasn't being drawn in some instances)
		super.onDraw(canvas);
		
		
		/*
		 * Get original clip rect from passed canvas object and
		 * bring it in by 5 pixels on the top and bottom.  This 
		 * was implemented to fix the gutter-line/line-number 
		 * over-draw cosmetic bug.
		 */
		
		canvas.getClipBounds(originalClipRect);
		drawingRect.set(originalClipRect);
		drawingRect.bottom -= 5;
		drawingRect.top += 5;
		canvas.clipRect(drawingRect, Op.REPLACE);
		
//		Log.i(TAG, "originalClipRect = " + originalClipRect.toShortString());
//		Log.i(TAG, "new drawingRect = " + drawingRect.toShortString());
		
		
		if ( isDrawingGutterLine() )
		{
			//  
			// DONE  1)  It draws a bit past text area, so we need to calculate the starting and ending position better
			// DONE  2)  When the text are becomes bigger than the initial size, the line moves.  So, we can not use
			//			 this.getHeight() to calculate the length of the line.  Need to find something which takes into
			//			 account the amount of text entered.  
			
			// Draw gutter line m_gutterSize - 4 from the left of the screen, top to bottom
//			Log.i(TAG, "----- this.computeVerticalScrollRange() = " + Integer.toString(this.computeVerticalScrollRange()));
			canvas.drawLine(m_gutterSize - 4, 0, m_gutterSize - 4, this.computeVerticalScrollRange(), m_gutterLinePaint);
		}
		
		if ( isDrawingLineNumbers() )
		{
			// Grab total line count
			lineCount = this.getLineCount();
			
			
			// Cycle through every line
			
			for ( int i = 0; i < lineCount; i++ )
			{
				this.getLineBounds(i, m_gutterLineRect);
				canvas.drawText(Integer.toString(i + 1), (float) m_gutterSize - 10, 
								(float) ((m_gutterLineRect.top + m_gutterLineRect.bottom) / 2 + (m_gutterTextPaint.getTextSize() / 2)), 
								m_gutterTextPaint);
			}
		}
		
		// restore standard clip rectangle
		canvas.clipRect(originalClipRect, Op.REPLACE);
		
		if ( m_highlightText )
		{
			highlightText();
		}
		
		
	}

	@Override
	public boolean onPreDraw() {
		return super.onPreDraw();
	}
	
	/**
	 * 
	 */
	
	private 	void	highlightText()
	{
		/*
		 *  // Get our EditText object.
			EditText vw = (EditText)findViewById(R.id.text);
			
			// Set the EditText's text.
			vw.setText("Italic, highlighted, bold.");
			
			// If this were just a TextView, we could do:
			// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
			// to force it to use Spannable storage so styles can be attached.
			// Or we could specify that in the XML.
			
			// Get the EditText's internal text storage
			Spannable str = vw.getText();
			
			// Create our span sections, and assign a format to each.
			str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		 */
		
		
	}

	/**
	 * Sets a flag if the text has changed.  NOTE: This method might also
	 * be useful to implement an undo feature!
	 */
	@Override
	protected void onTextChanged(CharSequence text, int start, int before,
			int after) {
		// TODO Auto-generated method stub
		super.onTextChanged(text, start, before, after);
		
		m_textHasChanged = true;
	}
	

}
